```{r boilerplate}
library(tidyverse)
library(ggrepel)
source("../code/pitr.R")
source("../sims/simple_sim.R")

knitr::opts_chunk$set(dev = "cairo_pdf")
```


```{r sample_data}
set.seed(323)

d <- 1
degree <- 2
n <- 10000
# beta <- matrix(rnorm(4 * (d+1 + 1)), ncol = 4) / 2
beta <- -matrix(rnorm(3 *  (choose(d + degree, degree) - 1)), ncol = 3) * 40
intercept <- c(.2, .15, 0)
beta <- rbind(intercept, beta, intercept, beta)
beta <- cbind(beta, 0)
pscore <- rep(.5, n)

shft <- 0.01

dat <- sample_rct(n, d, poly_pscore_model, beta = beta, degree = degree) %>%
  mutate(e10 = e10 - shft,
         across(c(e01, e11, e00), ~ . + shft / 3),
         m1 = e11 + e10,
         m0 = e11 + e01,
         tau = m1 - m0,
         tau_pos = tau >= 0,
         pos_class = m1 + m0 >= 1)

```

```{r component_plot, fig.width = 4.5, fig.height = 4.5}

dat %>%
  select(X1, m1, m0, e01) %>%
  pivot_longer(-X1) %>%
  ggplot(aes(x = X1, y = value, color = name)) +
  geom_line(size = 1) +
  scale_color_brewer("", type = "qual", palette = "Set1",
                     labels = expression(e[0][1](x), m(0,x), m(1,x))
                    # labels = expression(P(group("(", list(Y(1), Y(0)), ")") == group("(", list(0,1),")") ~ "|" ~ X == x),
                    #                     P(Y(0) == 1 ~ "|" ~ X == x),
                    #                     P(Y(1) == 1 ~ "|" ~ X == x))
                     ) +
  ylab("Probability") +
  xlab("Covariate") +
  theme_bw() +
  theme(legend.position = "bottom")

```
```{r bound_plot, fig.width = 4.5, fig.height = 4.5}
ug <- .8
ub <- 1


dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(oracle_rule = tau + (ug - ub) * e01,
         regret_nobody = ifelse(pos_class,
                                ub * m1 - ug * m0 + ug - ub,
                                ug * m1 - ub * m0),
        regret_oracle = case_when(
          regret_nobody >= 0 ~ tau,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ 2 * ug * m1 - (ug + ub) * m0,
          pos_class ~ (ug + ub) * m1 - 2 * ug * m0 + ug - ub
        )
  ) %>%
  select(X1, tau, oracle_rule, regret_nobody, regret_oracle, pos_class) %>%
  pivot_longer(-c(X1, pos_class)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("tau", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            ))) %>%
  ggplot(aes(x = X1, y = value, color = name)) +
  geom_line(aes(lty = name), size = 1) +
  # geom_ribbon(aes(ymax = value, ymin = 0, fill = trt)) +
  geom_hline(yintercept = 0, lty = 2) +
  scale_color_brewer("", type = "qual", palette = "Dark2",
                     labels = c("Symmetric Rule", "Asymmetric Rule",
                                # "Minimal Regret Relative to No Treatment",
                                "Minimal Regret Relative to Always Zero",
                                "Minimal Regret Relative to Oracle"
                                )) +
  guides(color = guide_legend(nrow = 2), lty = "none") +
  xlab("Covariate") +
  ylab("Decision function") +
  theme_bw() +
  theme(legend.position = "bottom")


```


```{r threshold_plot, fig.width = 4.5, fig.height = 4.5}
ug <- .8
ub <- 1


dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  filter(abs(X1) <= 4.5) %>%
  mutate(symmetric = 0,
         oracle_rule = (ub - ug) * e01,
         regret_nobody = ifelse(pos_class,
                                m1 - m0 - (ub * m1 - ug * m0 + ug - ub),
                                tau - (ug * m1 - ub * m0)),
        regret_oracle = case_when(
          tau >= regret_nobody ~ 0,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ tau - (2 * ug * m1 - (ug + ub) * m0),
          pos_class ~ tau - ((ug + ub) * m1 - 2 * ug * m0 + ug - ub)
        )
        )%>%
  select(X1, tau, symmetric, oracle_rule, regret_nobody, regret_oracle, pos_class) %>%
  pivot_longer(-c(X1, pos_class, tau)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("symmetric", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            ))) %>%
  ggplot(aes(x = X1, y = value, color = name)) +
  geom_line(lty = 2, size = 1) +
  geom_line(aes(y = tau), color = "black") +
  # geom_ribbon(aes(ymax = value, ymin = 0, fill = trt)) +
  # geom_hline(yintercept = 0, lty = 2) +
  scale_color_brewer("", type = "qual", palette = "Dark2",
                     labels = c("Symmetric Rule", "Asymmetric Rule",
                                # "Minimal Regret Relative to No Treatment",
                                "Minimal Regret Relative to Always Zero",
                                "Minimal Regret Relative to Oracle"
                                )) +
  guides(color = guide_legend(nrow = 2), lty = "none") +
  xlab("Covariate") +
  ylab("Treatment Effect") +
  ylim(c(-0.02, 0.15)) +
  theme_bw() +
  theme(legend.position = "bottom")


```


```{r component_plot_slides1, fig.width = 4.5, fig.height = 4.5}
maxx <- max(dat$X1)
dat %>%
  select(X1, m1, m0, e01) %>%
  pivot_longer(-X1) %>%
  mutate(label = case_when(X1 == maxx & name == "m0" ~ "Control",
                           X1 == maxx & name == "m1" ~ "Treatment",
                           X1 == maxx & name == "e01" ~ "Harm")) %>%
  ggplot(aes(x = X1, y = value, group = name, label = label, color = name)) +
  geom_line(size = 1, alpha = 0) +
  scale_color_manual("", values = c("#a6761d", "black", "black")) +
  guides(color = "none", alpha = "none") +
  scale_y_continuous("Probability", labels = scales::percent, limits = c(0,1)) +
  xlab("Covariate") +
  theme_minimal(10) +
  theme(legend.position = "bottom") -> p

p +
  geom_line(size = 1, data = . %>% filter(name == "m1")) +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name == "m1"), parse = T)
```

```{r component_plot_slides2, fig.width = 4.5, fig.height = 4.5}
p +
  geom_line(size = 1, data = . %>% filter(name != "e01")) +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name != "e01"), parse = T)
```

```{r component_plot_slides3, fig.width = 4.5, fig.height = 4.5}

p +
  geom_line(size = 1) +
  geom_label_repel(alpha = 0.6, parse = T)
```



```{r bound_plot_slides_oracle1, fig.width = 4.5, fig.height = 4.5}
ug <- .8
ub <- 1


dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(oracle_rule = tau + (ug - ub) * e01,
         regret_nobody = ifelse(pos_class,
                                ub * m1 - ug * m0 + ug - ub,
                                ug * m1 - ub * m0),
          regret_oracle = case_when(
          regret_nobody >= 0 ~ tau,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ 2 * ug * m1 - (ug + ub) * m0,
          pos_class ~ (ug + ub) * m1 - 2 * ug * m0 + ug - ub
        )) %>%
  select(X1, tau, oracle_rule, regret_nobody, regret_oracle) %>%
  pivot_longer(-c(X1)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("tau", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            )),
        label = case_when(X1 == maxx & name == "tau" ~ "Symmetric Rule",
                          X1 == maxx & name == "oracle_rule" ~ "Asymmetric Rule",
                          X1 == maxx & name == "regret_nobody" ~ "Minimal Regret Relative to Never Treat",
                          X1 == maxx & name == "regret_oracle" ~ "Minimal Regret Relative to Oracle")) %>%
  ggplot(aes(x = X1, y = value, color = name, label = label)) +
  geom_line(size = 1, alpha = 0) +
  scale_color_brewer("", type = "qual", palette = "Dark2") +
  geom_hline(yintercept = 0, lty = 2) +
  guides(color = "none") +
  xlab("Covariate") +
  ylab("Decision function") +
  ylim(c(-.25, .5)) +
  theme_minimal(10) +
  theme(legend.position = "bottom") -> oracle_p

oracle_p +
  geom_line(size = 1, data = . %>% filter(name == "tau"))  +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name =="tau"), segment.color = NA)

```

```{r bound_plot_slides_oracle2, fig.width = 4.5, fig.height = 4.5}

oracle_p +
  geom_line(size = 1, data = . %>% filter(name %in% c("tau", "oracle_rule")))  +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name %in% c("tau", "oracle_rule")), segment.color = NA)

```

```{r bound_plot_slides_oracle3, fig.width = 4.5, fig.height = 4.5}
ug <- .5
dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(oracle_rule = tau + (ug - ub) * e01,
         regret_nobody = ifelse(pos_class,
                                ub * m1 - ug * m0 + ug - ub,
                                ug * m1 - ub * m0),
          regret_oracle = case_when(
          regret_nobody >= 0 ~ tau,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ 2 * ug * m1 - (ug + ub) * m0,
          pos_class ~ (ug + ub) * m1 - 2 * ug * m0 + ug - ub
        )) %>%
  select(X1, tau, oracle_rule, regret_nobody, regret_oracle) %>%
  pivot_longer(-c(X1)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("tau", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            )),
        label = case_when(X1 == maxx & name == "tau" ~ "Symmetric Rule",
                          X1 == maxx & name == "oracle_rule" ~ "Asymmetric Rule",
                          X1 == maxx & name == "regret_nobody" ~ "Minimal Regret Relative to Never Treat",
                          X1 == maxx & name == "regret_oracle" ~ "Minimal Regret Relative to Oracle"))-> change_dat_low

oracle_p +
  geom_line(size = 1, data = change_dat_low %>% filter(name %in% c("tau", "oracle_rule")))  +
  geom_label_repel(alpha = 0.6, data = change_dat_low %>% filter(name %in% c("tau", "oracle_rule")))
```

```{r bound_plot_slides_oracle4, fig.width = 4.5, fig.height = 4.5}
ug <- 1.05
dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(oracle_rule = tau + (ug - ub) * e01,
         regret_nobody = ifelse(pos_class, # actually regret relative to everybody, to lazy to change for plotting
                                ub * m1 - ug * m0 + ug - ub,
                                ug * m1 - ub * m0),, 
          regret_oracle = case_when(
            tau > 0 ~ regret_nobody,
            regret_nobody < 0 ~ tau,
            !pos_class  ~ 2 * ug * m1 - (ug + ub) * m0,
            pos_class ~ (ug + ub) * m1 - 2 * ug * m0 + ug - ub
        )) %>%
  select(X1, tau, oracle_rule, regret_nobody, regret_oracle) %>%
  pivot_longer(-c(X1)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("tau", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            )),
        label = case_when(X1 == maxx & name == "tau" ~ "Symmetric Rule",
                          X1 == maxx & name == "oracle_rule" ~ "Asymmetric Rule",
                          X1 == maxx & name == "regret_nobody" ~ "Minimal Regret Relative to Always Treat",
                          X1 == maxx & name == "regret_oracle" ~ "Minimal Regret Relative to Oracle"))-> change_dat_hi
oracle_p +
  geom_line(size = 1, data = change_dat_hi %>% filter(name %in% c("tau", "oracle_rule")))  +
  geom_label_repel(alpha = 0.6, data = change_dat_hi %>% filter(name %in% c("tau", "oracle_rule")), segment.color = NA)
```


```{r bound_plot_slides_oracle5, fig.width = 4.5, fig.height = 4.5}

oracle_p +
  geom_line(size = 1, data = . %>% filter(name %in% c("tau", "oracle_rule")))

```

```{r bound_plot_slides_soc1, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = . %>% filter(name %in% c("tau", "oracle_rule", "regret_nobody")))  +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name %in% c("regret_nobody")), segment.color = NA)
```


```{r bound_plot_slides_soc2, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = change_dat_low %>% filter(name %in% c("tau", "oracle_rule", "regret_nobody")))  +
  geom_label_repel(alpha = 0.6, data = change_dat_low %>% filter(name %in% c("regret_nobody")), segment.color = NA)
```

```{r bound_plot_slides_soc3, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = change_dat_hi %>% filter(name %in% c("tau", "oracle_rule", "regret_nobody")))  +
  geom_label_repel(alpha = 0.6, data = change_dat_hi %>% filter(name %in% c("regret_nobody")), segment.color = NA)
```


```{r bound_plot_slides_soc4, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = . %>% filter(name %in% c("tau", "oracle_rule", "regret_nobody"))) +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name %in% c("tau", "oracle_rule", "regret_nobody")), segment.color = NA)
```


```{r bound_plot_slides_v_oracle1, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1)  +
  geom_label_repel(alpha = 0.6, data = . %>% filter(name == "regret_oracle"), segment.color = NA)
```


```{r bound_plot_slides_v_oracle2, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = change_dat_low)  +
  geom_label_repel(alpha = 0.6, data = change_dat_low %>% filter(name == "regret_oracle"), segment.color = NA)
```

```{r bound_plot_slides_v_oracle3, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = change_dat_hi)  +
  geom_label_repel(alpha = 0.6, data = change_dat_hi  %>% filter(name == "regret_oracle"), segment.color = NA)
```
```{r bound_plot_slides_v_oracle4, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, data = change_dat_hi)  +
  geom_label_repel(alpha = 0.6, data = change_dat_hi, segment.color = NA) +
  ylim(c(-.05, .1)) +
  xlim(c(-2, 2))
```


```{r threshold_plot_slides_oracle1, fig.width = 4.5, fig.height = 4.5}
ug <- .8
ub <- 1


dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(symmetric = 0,
         oracle_rule = (ub - ug) * e01,
         regret_nobody = ifelse(pos_class,
                                m1 - m0 - (ub * m1 - ug * m0 + ug - ub),
                                tau - (ug * m1 - ub * m0)),
        regret_oracle = case_when(
          tau >= regret_nobody ~ 0,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ tau - (2 * ug * m1 - (ug + ub) * m0),
          pos_class ~ tau - ((ug + ub) * m1 - 2 * ug * m0 + ug - ub)
        )
        )%>%
  select(X1, tau, symmetric, oracle_rule, regret_nobody, regret_oracle, pos_class) %>%
  pivot_longer(-c(X1, pos_class, tau)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("symmetric", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            )),
        label = case_when(X1 == maxx & name == "symmetric" ~ "Symmetric Rule",
                          X1 == maxx & name == "oracle_rule" ~ "Asymmetric Rule",
                          X1 == maxx & name == "regret_nobody" ~ "Minimal Regret Relative to Never Treat",
                          X1 == maxx & name == "regret_oracle" ~ "Minimal Regret Relative to Oracle")) %>%
  ggplot(aes(x = X1, y = value, color = name, label = label)) +
  geom_line(aes(y = tau), color = "#1b9e77", size = 1) +
  geom_line(size = 1, lty = 2, alpha = 0) +
  geom_hline(yintercept = 0, lty = 2, size = 1) +
  geom_label_repel(alpha = 0.6, aes(y = tau), label = "Effect", data = . %>% filter(X1 == maxx, name == "symmetric"), color = "#1b9e77") +
  scale_color_brewer("", type = "qual", palette = "Dark2") +
  guides(color = "none") +
  xlab("Covariate") +
  ylab("Decision function") +
  ylim(c(-0.05, .5)) +
  theme_minimal(10) +
  theme(legend.position = "bottom") -> oracle_p

oracle_p

```

```{r threshold_plot_slides_oracle2, fig.width = 4.5, fig.height = 4.5}

oracle_p +
  geom_line(size = 1, lty = 2, data = . %>% filter(name %in% c("oracle_rule")))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = . %>% filter(name %in% c("oracle_rule")), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = . %>% filter(name %in% c("oracle_rule")), segment.color = NA, seed = 2211)

```

```{r threshold_plot_slides_oracle3, fig.width = 4.5, fig.height = 4.5}
ug <- .5
dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(symmetric = 0,
         oracle_rule = (ub - ug) * e01,
         regret_nobody = ifelse(pos_class,
                                m1 - m0 - (ub * m1 - ug * m0 + ug - ub),
                                tau - (ug * m1 - ub * m0)),
        regret_oracle = case_when(
          tau >= regret_nobody ~ 0,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ tau - (2 * ug * m1 - (ug + ub) * m0),
          pos_class ~ tau - ((ug + ub) * m1 - 2 * ug * m0 + ug - ub)
        )
        )%>%
  select(X1, tau, symmetric, oracle_rule, regret_nobody, regret_oracle, pos_class) %>%
  pivot_longer(-c(X1, pos_class, tau)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("symmetric", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            )),
        label = case_when(X1 == maxx & name == "symmetric" ~ "Symmetric Rule",
                          X1 == maxx & name == "oracle_rule" ~ "Asymmetric Rule",
                          X1 == maxx & name == "regret_nobody" ~ "Minimal Regret Relative to Never Treat",
                          X1 == maxx & name == "regret_oracle" ~ "Minimal Regret Relative to Oracle")) -> change_dat_low

oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_low %>% filter(name %in% c( "oracle_rule")))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_low %>% filter(name %in% c("oracle_rule")), seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_low %>% filter(name %in% c("oracle_rule")), seed = 2211)
```

```{r threshold_plot_slides_oracle4, fig.width = 4.5, fig.height = 4.5}
ug <- 1.1
dat %>%
  mutate(idx = row_number()) %>%
  filter(idx %in% sample(idx, 1000) | abs(X1) >= 4) %>%
  mutate(symmetric = 0,
         oracle_rule = (ub - ug) * e01,
         regret_nobody = ifelse(pos_class,
                                m1 - m0 - (ub * m1 - ug * m0 + ug - ub),
                                tau - (ug * m1 - ub * m0)),
        regret_oracle = case_when(
          tau >= regret_nobody ~ 0,
          tau < 0 ~ regret_nobody,
          !pos_class  ~ tau - (2 * ug * m1 - (ug + ub) * m0),
          pos_class ~ tau - ((ug + ub) * m1 - 2 * ug * m0 + ug - ub)
        )
        )%>%
  select(X1, tau, symmetric, oracle_rule, regret_nobody, regret_oracle, pos_class) %>%
  pivot_longer(-c(X1, pos_class, tau)) %>%
  mutate(trt = value >= 0,
         name = factor(name, levels = c("symmetric", "oracle_rule",
                                            "regret_nobody", "regret_oracle"
                                            )),
        label = case_when(X1 == maxx & name == "symmetric" ~ "Symmetric Rule",
                          X1 == maxx & name == "oracle_rule" ~ "Asymmetric Rule",
                          X1 == maxx & name == "regret_nobody" ~ "Minimal Regret Relative to Never Treat",
                          X1 == maxx & name == "regret_oracle" ~ "Minimal Regret Relative to Oracle")) -> change_dat_hi
oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_hi %>% filter(name %in% c("oracle_rule")))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_hi %>% filter(name %in% c("oracle_rule")), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_hi %>% filter(name %in% c("oracle_rule")), segment.color = NA, seed = 2211)
```


```{r threshold_plot_slides_oracle5, fig.width = 4.5, fig.height = 4.5}

oracle_p +
  geom_line(size = 1, lty = 2, data = . %>% filter(name %in% c("oracle_rule")))

```

```{r threshold_plot_slides_soc1, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = . %>% filter(name %in% c("oracle_rule", "regret_nobody")))  +
  geom_label_repel(nudge_y = -0.02, data = . %>% filter(name %in% c("regret_nobody")), segment.color = NA, seed = 2211, alpha = 0.6)+
  geom_label_repel(nudge_y = -0.02, data = . %>% filter(name %in% c("regret_nobody")), segment.color = NA, fill = NA, seed = 2211)
```


```{r threshold_plot_slides_soc2, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_low %>% filter(name %in% c("oracle_rule", "regret_nobody")))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_low %>% filter(name %in% c("regret_nobody")), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_low %>% filter(name %in% c("regret_nobody")), segment.color = NA, seed = 2211)
```

```{r threshold_plot_slides_soc3, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_hi %>% filter(name %in% c("oracle_rule", "regret_nobody")))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_hi %>% filter(name %in% c("regret_nobody")), segment.color = NA, see = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_hi %>% filter(name %in% c("regret_nobody")), segment.color = NA, seed = 2211)
```


```{r threshold_plot_slides_soc4, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = . %>% filter(name %in% c("oracle_rule", "regret_nobody"))) +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = . %>% filter(name %in% c("symmetric", "oracle_rule", "regret_nobody")), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = . %>% filter(name %in% c("symmetric", "oracle_rule", "regret_nobody")), segment.color = NA, seed = 2211)
```


```{r threshold_plot_slides_v_oracle1, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2,  data = . %>% filter(name != "symmetric"))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = . %>% filter(name == "regret_oracle"), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = . %>% filter(name == "regret_oracle"), segment.color = NA, seed = 2211)
```


```{r threshold_plot_slides_v_oracle2, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_low %>% filter(name != "symmetric"))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_low %>% filter(name == "regret_oracle"), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_low %>% filter(name == "regret_oracle"), segment.color = NA, seed = 2211)
```

```{r threshold_plot_slides_v_oracle3, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_hi   %>% filter(name != "symmetric"))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_hi  %>% filter(name == "regret_oracle"), segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_hi  %>% filter(name == "regret_oracle"), segment.color = NA, seed = 2211)
```
```{r threshold_plot_slides_v_oracle4, fig.width = 4.5, fig.height = 4.5}
oracle_p +
  geom_line(size = 1, lty = 2, data = change_dat_hi  %>% filter(name != "symmetric"))  +
  geom_label_repel(alpha = 0.6, nudge_y = -0.02, data = change_dat_hi, segment.color = NA, seed = 2211) +
  geom_label_repel(fill = NA, nudge_y = -0.02, data = change_dat_hi, segment.color = NA, seed = 2211) +
  ylim(c(-.05, .1)) +
  xlim(c(-2, 2))
```








```{r prince_score_bounds_slides1, fig.width = 4.5, fig.height = 4.5}

dat %>%
  mutate(low = -tau * (1 - (tau >= 0)),
         hi = m0 + pos_class * (1 - m0 - m1)) %>%
  ggplot(aes(x = X1, y = e01, ymin = low, ymax = hi)) +
  scale_y_continuous("Probability", labels = scales::percent, limits = c(0,1)) +
  xlab("Covariate") +
  theme_minimal(10) +
  theme(legend.position = "bottom") -> p

p +
  geom_line(size = 1, color = "#a6761d")

```

```{r prince_score_bounds_slides2, fig.width = 4.5, fig.height = 4.5}
p + geom_ribbon(fill = "#a6761d", alpha = 0.5) +
  geom_line(size = 1, color = "#a6761d")
```